package com.nf.health.app.http;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nf.health.app.core.HttpActionHandle;
import com.nf.health.app.utils.BitmapUtils;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.Util;

import org.apache.http.Header;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

public class ImageUpLoader {

    public static final String FLAG = "uploadImage";
    private AsyncHttpClient client;
    private HttpActionHandle actionHandle;

    public ImageUpLoader(Context context, HttpActionHandle actionHandle) {
        this.client = new AsyncHttpClient();
        this.client.setTimeout(20 * 1000);
        this.actionHandle = actionHandle;
    }

    /**
     * @param path
     */

    public void uploadImage(String path) {
        uploadImage1(Apis.UPLOAD_URL, path);
    }


    /**
     * @param file
     * @param map
     */

    public void uploadImage(File file, Map<String, String> map) {
        uploadImage(Apis.UPLOAD_URL, file, map);
    }


    /**
     * @param url
     * @param file
     * @param map
     */
    public void uploadImage(String url, File file, Map<String, String> map) {
        RequestParams params = new RequestParams();
        try {
            for (String key : map.keySet()) {
                params.put(key, map.get(key));
            }
            params.put("userfile", file);
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
            LogUtil.d("yuyahao", "e2: " + e2.toString());
        }
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                actionHandle.handleActionStart(FLAG, null);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                actionHandle.handleActionFinish(FLAG, null);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            }

            @Override
            public void onSuccess(int statusCode, String content) {
                LogUtil.d("yuyahao", "e2: " + content.toString());
                handleMsg(content);
            }
        });
    }

    private void handleMsg(String content) {
//		String content = Arrays.toString(responseBody);
        try {
            if (content != null) {
                String imgMd5 = "";
                try {
                    imgMd5 = Util.getFirstUrlFromHtml(content);
                } catch (Exception e) {
                    imgMd5 = "";
                }
                if ("".equals(imgMd5)) {
                    actionHandle.handleActionError(FLAG, "图片上传失败", null);
                } else {
                    actionHandle.handleActionSuccess(FLAG, imgMd5);
                }
            } else {
                actionHandle.handleActionError(FLAG, "图片上传失败", null);
            }
        } catch (Exception e) {
            actionHandle.handleActionError(FLAG, "图片上传失败", null);
            e.printStackTrace();
        }
    }

    public void uploadImage1(String url, final String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap bitmap = BitmapUtils.compressImage(BitmapFactory.decodeFile(path,options));
        final String newpath = BitmapUtils.saveBitmap(bitmap, path);
        bitmap.recycle();
        RequestParams params = new RequestParams();
        try {
            params.put("userfile", new File(newpath));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                actionHandle.handleActionStart(FLAG, null);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                actionHandle.handleActionFinish(FLAG, null);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                actionHandle.handleActionError(FLAG, "图片上传失败", null);
            }

            @Override
            public void onSuccess(int statusCode, String content) {
                handleMsg(content);
            }
        });
    }
    /**
     * @param path
     */

    public void uploadImage3(String path) {
        uploadImage2(Apis.UPLOAD_URL, path);
    }

    /**
     * 不压缩，源文件上传
     * @param url
     * @param path
     */
    public void uploadImage2(String url, final String path) {
       BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        Bitmap bitmap = BitmapUtils.compressImage(BitmapFactory.decodeFile(path,options));
        final String newpath = BitmapUtils.saveBitmap(bitmap, path);
        bitmap.recycle();
        RequestParams params = new RequestParams();
        try {
            params.put("userfile", new File(path));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                actionHandle.handleActionStart(FLAG, null);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                actionHandle.handleActionFinish(FLAG, null);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                actionHandle.handleActionError(FLAG, "图片上传失败", null);
            }

            @Override
            public void onSuccess(int statusCode, String content) {
                handleMsg(content);
            }
        });
    }
}
